//
//  EditPersonInfoView.swift
//  maosheng
//
//  Created by lxl on 2019/8/14.
//

import UIKit

class EditPersonInfoView: UIView {
    let userBtn = UIButton(type: .custom)
    let nameField = UITextField()
    let descField = UITextField()
    let sexField = UITextField()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        configEditInfoView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func configEditInfoView() -> Void {
        let imgHeight = 80
        userBtn.layer.cornerRadius = CGFloat(imgHeight/2)
        userBtn.layer.masksToBounds = true
        userBtn.contentMode = .scaleAspectFit
        userBtn.setImage(UIImage(named: "camera"), for: .normal)
        userBtn.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        self.addSubview(userBtn)
        userBtn.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.width.height.equalTo(imgHeight)
            make.top.equalTo(20)
//            make.bottom.equalTo(-10)
        }
        let titleLab = UILabel()
        titleLab.text = "基本资料"
        self.addSubview(titleLab)
        titleLab.textAlignment = .left
        titleLab.textColor = KYP_333333_COLOR
        titleLab.font = UIFont(name: PF_Medium, size: 20)
        titleLab.snp.makeConstraints { (make) in
            make.left.equalTo(15)
            make.top.equalTo(userBtn.snp.bottom).offset(30)
        }
        
        let nameLab = UILabel()
        nameLab.text = "用户名"
        self.addSubview(nameLab)
        nameLab.textAlignment = .left
        nameLab.textColor = KYP_666666_COLOR
        nameLab.font = UIFont(name: PF_Regular, size: 15)
        nameLab.snp.makeConstraints { (make) in
            make.left.equalTo(titleLab)
            make.top.equalTo(titleLab.snp.bottom).offset(25)
        }
        
        
        nameField.textColor = KYP_666666_COLOR
        nameField.placeholder = "请填写您的昵称"
        nameField.font = UIFont(name: PF_Regular, size: 15)
        self.addSubview(nameField)
        nameField.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.left.equalTo(nameLab.snp.right).offset(40)
            make.centerY.equalTo(nameLab)
            make.height.equalTo(35)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = TABLE_LINE_COLOR
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.top.equalTo(nameLab.snp.bottom).offset(18)
            make.left.equalTo(titleLab)
            make.right.equalTo(-15)
        }
        
        let descLab = UILabel()
        descLab.text = "一句话介绍"
        self.addSubview(descLab)
        descLab.textColor = KYP_666666_COLOR
        descLab.font = UIFont(name: PF_Regular, size: 15)
        descLab.snp.makeConstraints { (make) in
            make.left.equalTo(titleLab)
            make.top.equalTo(nameLab.snp.bottom).offset(25)
        }
        
        descField.textColor = KYP_666666_COLOR
        descField.placeholder = "介绍自己的职业或者兴趣"
        descField.font = UIFont(name: PF_Regular, size: 15)
        self.addSubview(descField)
        descField.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.top.equalTo(descLab.snp.bottom).offset(5)
            make.left.equalTo(nameLab)
            make.height.equalTo(35)
        }
        
        let lineView1 = UIView()
        lineView1.backgroundColor = TABLE_LINE_COLOR
        self.addSubview(lineView1)
        lineView1.snp.makeConstraints { (make) in
            make.height.equalTo(0.5)
            make.top.equalTo(descField.snp.bottom)
            make.left.equalTo(titleLab)
            make.right.equalTo(-15)
        }
        
        
        
        
        let sexLab = UILabel()
        sexLab.text = "性别"
        self.addSubview(sexLab)
        sexLab.textAlignment = .left
        sexLab.textColor = KYP_666666_COLOR
        sexLab.font = UIFont(name: PF_Regular, size: 15)
        sexLab.snp.makeConstraints { (make) in
            make.left.equalTo(titleLab)
            make.top.equalTo(lineView1.snp.bottom).offset(18)
        }
        
        
        sexField.placeholder = "请选择"
        self.addSubview(sexField)
        sexField.textColor = KYP_666666_COLOR
        sexField.font = UIFont(name: PF_Regular, size: 15)
        sexField.snp.makeConstraints { (make) in
            make.left.equalTo(nameField)
            make.centerY.equalTo(sexLab)
        }
        
        let selectBtn = UIButton(type: .custom)
        selectBtn.setImage(UIImage(named: "edit_right"), for: .normal)
        selectBtn.contentHorizontalAlignment = .right
        selectBtn.addTarget(self, action: #selector(changeUserSex), for: .touchUpInside)
        addSubview(selectBtn)
        selectBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(sexField)
            make.right.equalTo(-15)
            make.height.equalTo(35)
            make.left.equalTo(titleLab)
        }
        
    }
    func setUserInfo(model : UserModel) -> Void {
        nameField.text = model.nickName
        descField.text = model.headline
        if let sex = model.gender{
            sexField.text = sex == 0 ? "女" : "男"
        }
        if let avater = model.avatar?.url{
            userBtn.kf.setBackgroundImage(with: URL(string: avater), for: .normal)
        }
        
    }
    //MARK:-更改用户i性别
    @objc func changeUserSex() -> Void {
        let alertController = UIAlertController(title: "", message: "",
                                                preferredStyle: .actionSheet)
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        let manAction = UIAlertAction(title: "男", style: .default) { (action) in
            self.sexField.text = action.title
        }
        let womanAction = UIAlertAction(title: "女", style: .default) { (action) in
            self.sexField.text = action.title
        }
        alertController.addAction(cancelAction)
        alertController.addAction(manAction)
        alertController.addAction(womanAction)
        let rootVC = UIApplication.shared.keyWindow?.rootViewController
        rootVC?.present(alertController, animated: true, completion: nil)
    }
    

}
